package com.example.connecting_dots_android;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import linkedList.LinkedList;
import view.GameMenu;
import exceptions.InvalidDataParameterException;
import exceptions.LinkedListIndexException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;

public class HighScores extends Activity {

	private static LinkedList highscoresList = new LinkedList();

	private static final File[] highscoreFiles = {
			new File("highscores_5x5.txt"), new File("highscores_7x7.txt"),
			new File("highscores_9x9.txt") };
	String readString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_high_scores);

		InputStream is = null;
		try {
			is = getAssets().open("highscores_5x5.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
		String total = readFromFile(is);
		TextView e = (TextView) findViewById(R.id.textView2);
		e.setText("Best in 5x5" + "\n" + total);

		try {
			is = getAssets().open("highscores_7x7.txt");
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		total = readFromFile(is);
		TextView e1 = (TextView) findViewById(R.id.textView1);
		e1.setText("Best in 7x7" + "\n" + total);

		try {
			is = getAssets().open("highscores_9x9.txt");
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		total = readFromFile(is);
		TextView e2 = (TextView) findViewById(R.id.textView3);
		e2.setText("Best in 9x9" + "\n" + total);

		// Show the Up button in the action bar.
		setupActionBar();

	}

	private String readFromFile(InputStream is) {
		BufferedReader r = new BufferedReader(new InputStreamReader(is));
		String x = "";
		String total = "";
		try {
			while ((x = r.readLine()) != null) {
				total += "\n";
				total += x;
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return total;

	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.high_scores, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
